<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            var trap = function (height) {
                let maxHeight = Math.max(...height)
                let maxIndex = height.findIndex((item) => item == maxHeight)
                let result = 0
                let leftHeight = height[0]
                let rightHeight = height[height.length - 1]
                for (let i = 0; i < maxIndex; i++) {
                    leftHeight = Math.max(leftHeight, height[i])
                    result += leftHeight - height[i]
                }
                for (let i = height.length - 1; i > maxIndex; i--) {
                    rightHeight = Math.max(rightHeight, height[i])
                    result += rightHeight - height[i]
                }
                return result
            }
        </script>
    </body>
</html>
